package com.jy.rhin.infra.repo.notfication.topic.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.rhin.infra.repo.notfication.topic.po.TopicPO;
import com.jy.rhin.infra.repo.notfication.topic.query.dto.TopicFullInfoDTO;
import com.jy.rhin.sdk.dto.notification.resp.EventTopicListDto;
import com.jy.rhin.sdk.dto.notification.resp.TopicInfoDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author WeiQ
 * @date 2022/6/28 16:15
 */
@Repository
public interface TopicMapper extends BaseMapper<TopicPO> {

    /**
     * 分页查询主题列表
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    @Select(value = " select a.id,                  \n" +
            " a.publisher_code as publisherCode,    \n" +
            " b.participant_name as publisherName,  \n" +
            " a.topic_name as topicName,            \n" +
            " a.topic_exp as topicExp,              \n" +
            " a.topic_desc as topicDesc,            \n" +
            " a.create_time as createTime           \n" +
            " from t_notification_topic a           \n" +
            " left join t_notification_participant b on a.publisher_code = b.participant_code and b.participant_role = 'PUBLISHER'  \n" +
            " ${ew.customSqlSegment} ")
    Page<TopicInfoDto> queryPage(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<Object> queryWrapper);


    /**
     * 发布主题数
     */
    @Select(value = " select count(*)" +
            " from t_notification_topic a           \n" +
            " left join t_notification_participant b on a.publisher_code = b.participant_code and b.participant_role = 'PUBLISHER'")
    Integer getTopicCount();

    /**
     * 查询主题详情
     *
     * @param topicId
     * @return
     */
    @Select("select a.id,                                                                       \n" +
            "       b.id                                         as queryId,                    \n" +
            "       c.id                                         as filterId,                   \n" +
            "       d.participant_name                           as publisherName,              \n" +
            "       d.participant_code                           as publisherCode,              \n" +
            "       a.topic_name                                 as topicName,                  \n" +
            "       a.topic_exp                                  as topicExp,                   \n" +
            "       a.topic_desc                                 as topicDesc,                  \n" +
            "       b.query_name                                 as queryName,                  \n" +
            "       b.query_exp                                  as queryExp,                   \n" +
            "       c.filter_name                                as filterName,                 \n" +
            "       c.filter_exp                                 as filterExp,                  \n" +
            "       c.filter_type                                as filterType,                 \n" +
            "       c.filter_code                                as filterCode,                 \n" +
            "       c.source_path                                as sourcePath,                 \n" +
            "       c.target_path                                as targetPath,                 \n" +
            "       a.create_time                                as createTime                  \n" +
            "from t_notification_topic a                                                        \n" +
            "  left join  t_notification_topic_query b on a.topic_exp = b.topic_exp             \n" +
            "  left join  t_notification_topic_query_filter c on  a.topic_exp = c.topic_exp     \n" +
            "               and b.query_exp = c.query_exp                                       \n" +
            "  left join  t_notification_participant d on d.participant_role = 'PUBLISHER'      \n" +
            "               and d.participant_code = a.publisher_code                           \n" +
            "where a.id = #{topicId}                                         ")
    List<TopicFullInfoDTO> queryTopicDetail(@Param("topicId") Long topicId);

    /**
     * 事件主题订阅数top10
     *
     * @param queryWrapper
     * @return
     */
    @Select(" select a.id,                                                                    \n" +
            "   a.publisher_code as publisherCode,                                            \n" +
            "   b.participant_name as publisherName,                                          \n" +
            "   a.topic_name as topicName,                                                    \n" +
            "   (SELECT COUNT(1) FROM t_notification_subscribe t1                             \n" +
            "   WHERE t1.topic_exp = a.topic_exp and                                          \n" +
            "    t1.status <> 'CANCEL') AS subscriptions,                                     \n" +
            "   a.create_time as createTime                                                   \n" +
            " from t_notification_topic a                                                     \n" +
            " left join t_notification_participant b on                                       \n" +
            "   a.publisher_code = b.participant_code and b.participant_role = 'PUBLISHER'    \n" +
            " ${ew.customSqlSegment} ")
    List<EventTopicListDto> queryEventTopicList(@Param(Constants.WRAPPER) QueryWrapper<Object> queryWrapper);

}
